package my.service;

import com.anna.config.Ioc.Service;
import my.model.User;

/**
 * Created by rajab on 17/4/13.
 */
@Service
public class UserService {
    public enum LoginResult {
        LOGIN_OK,
        INPUT_INVALID,
        PASSWORD_WRONG,
        USER_NOT_EXIST
    }

    public enum RegResult {
        REG_OK,
        INPUT_INVALD,
        USER_ALREADY_EXIST
    }

    public static LoginResult checkLogin(String username, String password) {
        if (username.isEmpty() || password.isEmpty()) {
            return LoginResult.INPUT_INVALID;
        } else {
            User user = User.dao.findFirst("SELECT * FROM user WHERE username = ?", username);
            if (user == null)
                return LoginResult.USER_NOT_EXIST;
            else if (user.getPassword().equals(password))
                return LoginResult.LOGIN_OK;
            else
                return LoginResult.PASSWORD_WRONG;
        }
    }

    public static RegResult checkReg(String username, String password) {
        if (username.isEmpty() || password.isEmpty())
            return RegResult.INPUT_INVALD;
        else {
            User user = User.dao.findFirst("SELECT * FROM user WHERE username = ?", username);
            if (user == null)
                return RegResult.REG_OK;
            else
                return RegResult.USER_ALREADY_EXIST;
        }
    }

    public static User findUserByUsername(String username){
        return User.dao.findFirst("SELECT * FROM user WHERE username = ?",username);
    }

    // public abstract User createUser(String username, String password, String role);
}
